//左侧导航hover
var Left_Nav_hover=$('.Js-left-nav-hover')
var NavList=Left_Nav_hover.attr('data-menu-list');
var NavLi=Left_Nav_hover.attr('data-menu-li');
function Left_Nav_Active(text_list,text_li){
	if(text_list){
		$('[data-text="'+text_list+'"]').addClass('active')
	}
	if(text_li){
		$('[data-text="'+text_li+'"]').addClass('active')
	}
	
}
Left_Nav_Active(NavList,NavLi)

//地址栏传值
function GetQueryString(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
}

//$('#Vue-Body').fadeIn(0)
var dev=GetQueryString('dev')
if(dev==1){$('#Vue-Body').fadeIn(0)}

//通过地址传值获取发布的类型，判断隐藏项是否显示
//var pages=GetQueryString("page");
//获取今天日期
function DateToday(AddDayCount){
	var data = new Date();
	if(!AddDayCount){
		AddDayCount=0;
	}
	data.setDate(data.getDate()+AddDayCount);//获取AddDayCount天后的日期 
	var y = data.getFullYear();
	var m = data.getMonth()+1;//获取当前月份的日期 
	var d = data.getDate();
	var time=y+"-"+m+"-"+d;
	return time;
}
//日期选择
var datepicker=$('.Js-datepicker').length;

function DatepiCker(){
	if(!datepicker){return false;}
	$('.Js-datepicker').datepicker({
		format: 'yyyy-mm-dd'
	});
}
//获取placeholder文字
function Placeholder_Text(id){
	var txt=$(id).attr('placeholder')
	return txt;
}
//回车事件
function Enter_Ajax(Key,click){
	console.log('Key')
	$(Key).keydown(function(e){
		console.log(Key)
		var curKey = e.which; 
		if(curKey == 13){ 
			$(click).click();
			return; 
		} 
	});
}
//ajax方法
function getData(data,ajax_fun) {
	console.warn('getData:ajax数据获取事件');
	return ajaxBaseFun('get',data,ajax_fun)
}
function postData(data,ajax_fun) {
	console.warn('postData:ajax数据提交事件');
	return ajaxBaseFun('post',data,ajax_fun)
}
function ajaxBaseFun(ajax_type,ajax_data,ajax_fun) {
	console.warn('ajaxData:ajax公共执行事件 ajax_type='+ajax_type);
	console.log(ajax_data)
	console.log(ajax_fun)
	var api=ajax_data.api;//请求地址
	var form_data=ajax_data.form_data;//请求参数
	var tips=ajax_data.tips;//请求成功提示
	var to_url=ajax_data.to_url;//跳转url
	var loading=ajax_data.loading;//加载中提示
	var data;
	console.log(form_data)
	//定义没有传值时的默认值
	if(tips===undefined)tips=true;//默认都提示
	if(to_url===undefined)to_url=false;//默认不跳转
	if(loading===undefined)loading=true;//默认显示动画
	//开始动画
	var load_index;
	if(ajax_type=='post'){
		//post才执行动画
		if(loading){
			load_index=loadingCG()
		}
	}
	//ajax同步执行
	//$.ajaxSetup({
		//async:false
	//});
	var call_fun=function(res) {
		console.log(res)
		var code=res.code;
		var msg=res.msg;
		data=res.data;
		//授权
		if (code==-1) {
			msgTips('登录信息失效,请重新登录',2);
			toLogin()
		}else if(code==1){
			if(tips){
				if(tips===true){
					msgTips(msg,1)//默认提示
				}else if(tips===false){
					//不提示
				}else{
					msgTips(tips,1)//自定义提示
				}
			}
			if(ajax_fun){
				console.warn('ajaxData:ajax公共执行事件-传递的函数 ajax_fun');
				console.log(data)
				ajax_fun(data);
				console.log('我是main.js')
				console.log(data)
				$('#Vue-Body').fadeIn(0)
				var size=$('.tooltips').size()
				if(size){
					$('.tooltips').tooltip();
				}
			}
			if(to_url){
				setTimeout(function(){
					location.href = to_url;//跳转
				},3000)
			}
		}else{ //如果有其他code 继续添加else if
			msgTips(msg,2)
		}
	};
	if(ajax_type=='post') {
		$.post(api,form_data,call_fun);
	} else {
		$.get(api,form_data,call_fun);
	}
	if (load_index) {
		layer.close(load_index);//关闭动画
	}
	console.warn('ajax:返回数据');
	console.log(data)
	return data;
}
//全局提示消息框
function msgTips(msg,icon) {
	var index=layer.msg(msg,{icon:icon,offset:'10px',time:3000});
	return index;
}
//加载中动画
function loadingCG(){
	//loading层
	var index=layer.load(1,{//0代表加载的风格，支持0-2
		shade:[0.2,'#fff']//0.1透明度的白色背景
	});
	return index;
}
//登录
function toLogin() {
	//window.location.href = '/admin/login/index'; //跳转登录
	return false;
}
//复制粘贴
function jsCopy(){ 
	var e=document.getElementById("Js-Copy-text");//对象是contents 
	e.select(); //选择对象 
	document.execCommand("Copy"); //执行浏览器复制命令
	msgTips('链接地址复制成功',1)
} 
//图片上传
function Img_Upload(name,fun){
	console.log('Img_Upload')
	var This=$('#file_'+name);
	var img=This.siblings('img')
	$('#file_'+name).fileupload({  
        url:'/api/common/upload-img',
	    dataType:'json',
	    progressall: function (e,data) {//设置上传进度事件的回调函数  
	    	//loadingCG()
	    	console.log(data)
        },done:function (e,data) {
        	console.log('data')
        	console.log(data.result)
	    	//设置文件上传完毕事件的回调函数  
	    	if (data.result.code == 1) {
	            var url = data.result.data.url;
	            img.attr('src',url);
	            $('#'+name).val(url);
	            if(fun){
	            	//执行函数-图片地址更新到vue数组
	            	fun($('#'+name),name,url)
	            }
			}else{
	            layer.msg(data.result.msg, {icon:2,offset:'10px'})
	        }
	        layer.closeAll('loading');
	    }
    })
}
//复选组
$(document).on('click','.Js-checkbox li',function(){
	var This=$(this);
	This.toggleClass('active')
	var List=This.parent('.Js-checkbox')
	var Name=List.attr('data-name')
	var Val=This.attr('data-'+Name)
	var Each_data=[];
	List.children('.active').each(function(){
		var Vals=$(this).attr('data-'+Name)
		Each_data.push(Vals)
	})
	List.attr('data-'+Name,Each_data);
})
$(document).on('click','.Js-layer-colse',function(){
	layer.closeAll();
	var id=$(this).attr('data-id')
	console.log(111111111111)
	console.log(id)
	if(id=='add'){
		MJS_URLArry.id='',
		URL_Make(MJS_URLArry)
	}
})

//日期选择
function DatePicker(id){
	$(id).datepicker({
		format:'yyyy-mm-dd'
	});
}
//退出登录
$(document).on('click','.Js-logout-a',function(){
	var ajax_data={
		api:'/adminapi/system/logout',
		tips:'退出登录成功',
		to_url:'/admin/login/index',
	}
	postData(ajax_data)
})


//获取分类
function Info_Class_List(api){
	var ajax_class_data={
		api:'/adminapi/system/class-'+api+'-list',
		form_data:{},
		tips:false,
	}
	getData(ajax_class_data,function(class_data){
		var List=class_data[0];
		var Class_list=List.nav_li;
		var Status=List.status;
		vm[api+'_list']=Class_list;
		vm[api+'_status']=Status;
	})
}


function If_Time_Max(Start_Time,End_Time){  
    var start=new Date(Start_Time.replace("-", "/").replace("-", "/"))  
    var end=new Date(End_Time.replace("-", "/").replace("-", "/"))
    console.log('2个时间比较大小')
    console.log(Start_Time+'---'+End_Time)
    console.log(start+'---'+end)
    if(start<end){  
        return 9;
    }else if(start>end){  
        return 6;
    }else{
        return 8; 
    }
}  
var Data_field_type_list=[
	//{id:1,type_text:'1v1'},
	//{id:2,type_text:'2v2'},
	//{id:3,type_text:'3v3'},
	//{id:4,type_text:'4v4'},
	{id:5,type_text:'5v5'},
	{id:6,type_text:'6v6'},
	{id:7,type_text:'7v7'},
	{id:8,type_text:'8v8'},
	{id:9,type_text:'9v9'},
	{id:10,type_text:'10v10'},
	{id:11,type_text:'11v11'},
]
var Data_field_time_list=[
	{id:0,start_time:'09:00',end_time:'11:00',time:'09:00-11:00'},
	{id:1,start_time:'11:00',end_time:'13:00',time:'11:00-13:00'},
	{id:2,start_time:'13:00',end_time:'15:00',time:'13:00-15:00'},
	{id:2,start_time:'15:00',end_time:'17:00',time:'15:00-17:00'},
	{id:2,start_time:'17:00',end_time:'19:00',time:'17:00-19:00'},
	{id:2,start_time:'19:00',end_time:'21:00',time:'19:00-21:00'},
	{id:2,start_time:'21:00',end_time:'23:00',time:'21:00-23:00'},
]
var Data_month_list=[
	{id:1,month:1,month_text:'一月'},
	{id:2,month:2,month_text:'二月'},
	{id:3,month:3,month_text:'三月'},
	{id:4,month:4,month_text:'四月'},
	{id:5,month:5,month_text:'五月'},
	{id:6,month:6,month_text:'六月'},
	{id:7,month:7,month_text:'七月'},
	{id:8,month:8,month_text:'八月'},
	{id:9,month:9,month_text:'九月'},
	{id:10,month:10,month_text:'十月'},
	{id:11,month:11,month_text:'十一月'},
	{id:12,month:12,month_text:'十二月'},
]
var URLArry={};//url参数值数组 请求参数
function URL_Search(){
	console.warn('Page.js URL地址栏参数值获取');
	var name,value;
	var str=location.href;//取得整个地址栏
	var num=str.indexOf("?")
	str=str.substr(num+1);//取得所有参数   stringvar.substr(start[,length ]
	var arr=str.split("&");//各个参数放到数组里
	for(var i=0;i < arr.length;i++){
    	num=arr[i].indexOf("=");
		if(num>0){
			name=arr[i].substring(0,num);
			value=arr[i].substr(num+1);
			if(arr[i].indexOf("title=")==0){
				var url=decodeURI(location.href);
				value=url.split('&')[i].split('=')[i];
				if(value==undefined){value='';}
			}
			this[name]=value;
			URLArry[name]=value;//设置url数组
		}
	}
	if(!this['page']){
		this['page']=1;
		URLArry['page']=1;
	}
	if(!this['page_num']){
		this['page_num']=10;
		URLArry['page_num']=10;
	}
}
URL_Request = new URL_Search();//实例化
console.log('page.js')
console.log(URLArry)
console.log(URL_Request)
function URL_Make(URL_data){
	console.warn('Page.js URL地址生成');
	var data=URL_data;
	//获取不带参数url
	var URL_Make=location.origin+location.pathname;
	for(var name in data){
		var URL_New;
		var URL_Symbol='&';
		console.log(name)
		console.log(data[name])
		// 
		if(name!='page_num'){
		if(name!='order_time'){
			URLArry[name]=data[name];//设置url数组
			if(name=='page_id'){
				URL_Symbol='?';
			}
			URL_New=URL_Symbol+name+'='+data[name];//设置新键值对url
			URL_Make+=URL_New;//生成url
		}
		}
		//console.log(URL_New)
		//console.log(URL_Make)
	}
	Edit_History_PushState(URL_Make);//设置url地址栏
}
function AD_List_Edit(name,val){
	console.warn('Page.js ajax请求参数值更新');
	console.info('name='+name+' val='+val);
	URLArry[name]=val;
	AD_list.form_data=URLArry;
	console.log('Page.js ajax请求参数值更新 AD_list.form_data')
	console.log(URLArry)
	console.log(AD_list.form_data)
}
function Edit_History_PushState(url){
	console.log('url')
	console.log(url)
	console.warn('Page.js Http_Url:设置url为 '+url);
	history.pushState(null,'',url);
}
//vue列表回车状态改变&&删除-全局方法
function Vm_List_Status(ajax_data,page_type){
	console.info('Page.js Vm_List_Status:全局方法-删除');
	console.info(ajax_data);
	console.info(page_type);
	var Status_Ajax=function(page_type){
		postData(ajax_data,function(){
		var length=vm.lists.length;
		var page=parseInt(AD_list.form_data.page);
		console.log(1111111111111)
		console.log(vm)
		if(length==0){
			if(page!=1){page=page-1;}
		}
		console.log(1111111111111)
		console.log(page)
		//刷新数据
		if(!page_type){Vm_List_Get_Data(page)}
		})
	}
	Confirm_Ajax(ajax_data,Status_Ajax)
	Confirm_Enter_Ajax($(this),ajax_data,Status_Ajax)
}

//弹框确认是否执行
function Confirm_Ajax(data,ajax_fun){
	console.warn('Page.js Confirm_Ajax:弹框确认是否执行事件');
	console.log(data)
	var msg=data.msg;//弹框提示文字
	var form_data=data.form_data;//ajax提交数据
	layer.confirm(msg,{
		title:'提示信息',
		btn: ['是','取消'] //按钮
	},function(index){
		ajax_fun();
		console.info('-------------------------点击删除')
		layer.close(index)
	}, function(){
		layer.closeAll()
	})
}
//回车删除,esc取消删除
function Confirm_Enter_Ajax(This,data,ajax_fun){
	console.warn('Page.js Confirm_Enter_Ajax:回车删除,esc取消删除');
	console.log(data)
	//点击后失去焦点
	This.blur()
	var i=1;//*冒泡多次执行,初始化1
	$(document).on('keydown','*',function(e){
		var curKey = e.which; 
		if(curKey == 13){
			if(i!=1){return}
			ajax_fun()
			console.info('-------------------------回车删除')
			i++
			return false; 
		}
		if(curKey == 27){ 
			console.info('-------------------------取消删除')
			layer.closeAll()
			return false; 
		} 
	});
}